﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Caching;
using MPR.Poker.DALEntity;

namespace MPR.Poker.DALEntity.DataCache
{
    public class CacheProvider
    {
        public const string CacheKey = "PokerLife";
        public const string CacheKey_User = CacheKey + "_User";
        public const string CacheKey_MemberType = CacheKey + "_MemberType";
        public const string CacheKey_Member = CacheKey + "_Member";

        public const string CacheKey_Currency = CacheKey + "_Currency";
        public const string CacheKey_Country = CacheKey + "_Country";
        public const string CacheKey_Province = CacheKey + "_Province";
        public const string CacheKey_Prize = CacheKey + "_Prize";

        public const string CacheKey_Tournament = CacheKey + "_Tournament";

        private static ObjectCache Cache { get { return MemoryCache.Default; } }

        public static object Get(string key)
        {
            return Cache[key];
        }

        public static void Set(string key, object data)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(DAL_Globals.cacheTime);    
            
            Cache.Add(new CacheItem(key, data), policy);
        }

        public static void Set(string key, object data, int cacheTime)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);

            Cache.Add(new CacheItem(key, data), policy);
        }

        public static bool IsSet(string key)
        {
            return (Cache[key] != null);
        }

        public static void Invalidate(string key)
        {
            Cache.Remove(key);
        }

        public static string SetCacheKeyCombo(string CacheKey)
        {
            return CacheKey + "_Combo";
        }
    }
}
